# Create your views here.
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.contrib.auth.models import User
from cartoon_us.accounts.models import friend
from cartoon_us.user_profile.models import UploadFileForm
from cartoon_us.comment.models import comment
from cartoon_us.accounts.models import vote

def my(request,u_ser):
    data={}
    if request.user.is_authenticated():
        #are you logged in
        if u_ser==request.user.username:
            data['edit']=True
        else:
            data['edit']=False
        data['user']=u_ser
        data['me']=request.user.username
        try:
            u=User.objects.get(username=u_ser)
        except:
            return HttpResponseRedirect('../home') # user doesnt exist
        data['date_joined']=u.date_joined
        m=User.objects.get(username=u_ser)
        #get some data about the user
        data['friends']=m.friend_set.all()[:5]
        try:
            k=vote.objects.get(on=u_ser)
            #user has a rating already
            data['rating']=k.score()
        except:
            data['rating']=0
            # new user rating
        com=[]
        #get comments on this user on the proifle page
        for c in comment.objects.all():
            if c.on=='profile' and c.to==u_ser:
                com.append(c)
        data['comments']=com
        data['myclans']=m.my_clans_set.all()[:5]
        data['myillustrations']=m.illustration_set.all()[:5]
        # show the 5 most recent as sorted by the database
        tt=m.story_set.all()[:5]
        for t in tt:
            t.work.abstract=t.work.abstract[0:100]
        data['myfictions']=tt
        data['mycomics']=m.comics_set.all()[:5]
        return render_to_response('profile/profile_total.html',data)
    else:
        return HttpResponseRedirect('../../home') # must be logged in to see profile
def add(request,u_ser):
    if request.user.is_authenticated():
        me=request.user
        if u_ser == me.username:
            return HttpResponseRedirect(request.META['HTTP_REFERER'])
        #send them back to where they were
        else:
            try:
                me.friend_set.get(user=u_ser)
                return HttpResponseRedirect(request.META['HTTP_REFERER'])
            # see if we are already friends
            except:
                me.friend_set.create(user=u_ser)
                #other wise add them to the friend list
                me.save()
                return HttpResponseRedirect(request.META['HTTP_REFERER'])
    else:
        return HttpResponseRedirect('../'+u_ser)
def rate(request,user):
    if request.user.is_authenticated():
        try:
            k=vote.objects.get(on=user)
            k.rate+=int(request.POST['rate'])
            k.count+=1
            k.save()
            # vote count and score incrment
            return HttpResponseRedirect(request.META['HTTP_REFERER'])
        # allows voting from any page
        except:
            k=vote(on=user,count=1,rate=request.POST['rate'])
            k.save()
            # save the rating as a new rating
            return HttpResponseRedirect(request.META['HTTP_REFERER'])
    return HttpResponseRedirect('../../home')
def uploadpic(request,u_ser):
    if request.method != 'POST':
        return HttpResponseRedirect('../../home')
    path='C:/SDD/trunk/image/profile_pic/'+u_ser+'.jpg'
    destination=open(path,'wb+')
    #write the file to the server
    for chunk in request.FILES['pic'].chunks():
        destination.write(chunk)
    destination.close()
    #save
    return HttpResponseRedirect('../'+u_ser)
def Comment(request,on,user):
    if request.user.is_authenticated():
        if request.method != 'POST':
            return HttpResponseRedirect('../../home')
        c=comment(userComment=request.POST['comment'],
                  poster=request.user.username,
                  to=user,
                  on=on)
        c.save()
        # save the comments
        return HttpResponseRedirect(request.META['HTTP_REFERER'])
    #allow the comments to be submitted from any page
    else:
        return HttpResponseRedircet('../../home')
